//
//  MSChapterScene.m
//  MagicSword
//
//  Created by Netease on 15/11/16.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSChapterScene.h"
#import "MSChapterDotInfo.h"
#import "MSDBManager.h"
#import "MSChapterManager.h"
#import "MSAlertChapterView.h"
#import "MSFightGameScene.h"
#import "MSShadowLabel.h"

#define NodeNameIndex 1234
@implementation MSChapterScene {
    NSInteger  _session;
    NSInteger  _chapter;
    NSArray   *_dotArray;
    NSInteger _passIndex;
    NSArray   *_dotNameArray;
    MSAlertChapterView *_alertView;
    NSMutableArray *_dotSpriteArray;
    SKSpriteNode *_backgroundView;
}

- (id)initWithSession:(NSInteger)session {
    self = [super init];
    if (self) {
        _session = session;
        _passIndex = 0;
        
        
        
        
    }
    return self;
}

- (void)didMoveToView:(SKView *)view {
    /* Setup your scene here */
    [super didMoveToView:view];
    [self.topBarNode showBackNode:YES];
    
    [self initDotsData];
    [self initSubNodes];
}

- (void)initDotsData {
    if (!_dotSpriteArray) {
        _dotSpriteArray = [NSMutableArray array];
    } else {
        [_dotSpriteArray makeObjectsPerformSelector:@selector(removeFromParent)];
        [_dotSpriteArray removeAllObjects];
    }
    [self initPassIndex];
    _dotNameArray = [MSChapterManager getChapterName];
    _dotArray = [MSChapterManager getDotDataWithSession:_session passIndex:_passIndex];
}

- (void)initPassIndex {
    NSInteger passSession = [[MSDBManager shareManager] getPassSession];
    if (passSession == _session) {
        _passIndex = [[MSDBManager shareManager] getPassChapter];
    } else if (passSession > _session) {
        _passIndex = 12;
    } else {
        _passIndex = 0;
    }
}

- (void)initSubNodes {
    // 背景
    if (!_backgroundView) {
        _backgroundView = [SKSpriteNode spriteNodeWithImageNamed:@"main_session1_background"];
        _backgroundView.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
        _backgroundView.size = self.size;
        _backgroundView.zPosition = 2;
        [self addChild:_backgroundView];
    }
    
    [self.topBarNode setTitle:[MSChapterManager getSessionName:_session]];
    
    int i=0;
    for (MSChapterDotInfo *info in _dotArray) {
        NSString *imageName = @"main_nopass_dot";
        if (info.model == MSChapterDotModel_In) {
            imageName = @"main_in_dot";
        } else if (info.model == MSChapterDotModel_Pass) {
            imageName = @"main_pass_dot";
        }
        
        SKSpriteNode *dot = [SKSpriteNode spriteNodeWithImageNamed:imageName];
        dot.position = info.possion;
        dot.name = [NSString stringWithFormat:@"%d", NodeNameIndex + i];
        dot.zPosition = 3;
        [self addChild:dot];
        
        MSShadowLabel *lable = [MSShadowLabel labelNodeWithFontNamed:@"Chalkduster"];
        [lable setPosition:CGPointMake(25, -5)];
        lable.name = [NSString stringWithFormat:@"%d", NodeNameIndex + i];
        [lable setFontSize:11];
        [dot addChild:lable];
        
        if (_dotNameArray.count > i) {
            [lable setText:_dotNameArray[i]];
        }
        i++;
        [_dotSpriteArray addObject:dot];
    }
    
    if (_dotArray.count > _passIndex) {
        MSChapterDotInfo *info = _dotArray[_passIndex];
        SKSpriteNode *flag = [SKSpriteNode spriteNodeWithImageNamed:@"main_redflag"];
        flag.position = CGPointMake(info.possion.x+15, info.possion.y+15);
        flag.zPosition = 4;
        flag.name = [NSString stringWithFormat:@"%d", NodeNameIndex + (int)_passIndex];
        [self addChild:flag];
        [_dotSpriteArray addObject:flag];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];
    
    NSInteger index = [touchedNode.name integerValue] - NodeNameIndex;
    if (index>=0 && index <= _passIndex) {
        if (!_alertView) {
            _alertView = [[MSAlertChapterView alloc] init];
            _alertView.hidden = YES;
            _alertView.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
            [self addChild:_alertView];
        }
        _alertView.hidden = NO;
        NSString *name = [NSString stringWithFormat:@"%@ %@", [MSChapterManager getSessionName:_session], _dotNameArray[index]];
        [_alertView.nameLabel setText:name];
        [_alertView.prizeLabel setText:[NSString stringWithFormat:@"战利品:%@", [MSChapterManager getPrizeMessageWithSession:_session index:index]]];
        [_alertView.winLabel setText:[NSString stringWithFormat:@"获胜条件:%@", @"消灭所有敌人"]];
        _chapter = index;
    }
    
    // 关闭武器窗口
    if ([[touchedNode name] isEqualToString:NodeName_CloseAlertNodes]) {
        if (_alertView) {
            [_alertView setHidden:YES];
        }
    }
    
    // 开始战斗
    if ([[touchedNode name] isEqualToString:NodeName_DoneWeaponAlertNodes]) {
        MSFightGameScene *gameScene = [[MSFightGameScene alloc] initWithSession:_session Chapter:_chapter];
        gameScene.size = self.frame.size;
        gameScene.scaleMode = SKSceneScaleModeAspectFill;
        [self.view pushScene:gameScene];
        
        if (_alertView) {
            [_alertView setHidden:YES];
        }
    }
}

@end
